package {{ configuration.basePackage }}.service.{{ configuration.module }};

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studyllm.entity.{{ configuration.module }}.{{configuration.javaName}};
import com.example.studyllm.mapper.{{ configuration.module }}.{{configuration.javaName}}Mapper;
import com.example.studyllm.service.base.BaseServiceImpl;
import com.example.studyllm.utils.BeanUtils;
import com.example.studyllm.vo.base.PageResultVO;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;

/**
 * <p>
 *  {{configuration.tableCommont}} service实现类
 * </p>
 *
 * @author kw
 * @since {{configuration.date}}
 */
@Service
public class {{configuration.javaName}}Service extends BaseServiceImpl<{{configuration.javaName}}Mapper, {{configuration.javaName}}> {


    public Boolean add{{configuration.javaName}}({{configuration.javaName}} {{ configuration.lowerTableName }}) {
        return this.save({{ configuration.lowerTableName }});
    }

    public Boolean update{{configuration.javaName}}ById({{configuration.javaName}} {{ configuration.lowerTableName }}) {
        return this.updateById({{ configuration.lowerTableName }});
    }

    public PageResultVO<{{configuration.javaName}}> getPage({{configuration.javaName}} pageParam) {

        LambdaQueryWrapper<{{configuration.javaName}}> lambda = new QueryWrapper<{{configuration.javaName}}>().lambda();
        this.buildCondition(lambda, pageParam);{% for item in datas %}{% if item.Field == 'id' %}
        lambda.orderBy(true, false, {{configuration.javaName}}::getId);{% endif %} {% endfor %}
        PageResultVO<{{configuration.javaName}}> resultPage = BeanUtils.changePage({{configuration.javaName}}.class, this.page(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), lambda));
        return resultPage;
    }
    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<{{configuration.javaName}}> lambda, {{configuration.javaName}} param) {
        {% for item in datas %}
            lambda.eq(ObjectUtil.isNotEmpty(param.get{{ item.FieldUper }}()),{{configuration.javaName}}::get{{ item.FieldUper }}, param.get{{ item.FieldUper }}());
        {% endfor %}
    }
}
